package main

import (
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"net/http"
)

// Rewrite 中间件会根据提供的规则重写URL路径，它更适用于向后兼容与创建更清晰、更具描述性的链接。
func main() {
	e := echo.New()

	//e.Pre(middleware.Rewrite(map[string]string{
	//	"/old":              "/new",
	//	"/api/*":            "/$1",
	//	"/js/*":             "/public/javascripts/$1",
	//	"/users/*/orders/*": "/user/$1/order/$2",
	//}))

	e.Pre(middleware.RewriteWithConfig(CustomRewriteConfig()))
	e.Use(middleware.Logger())
	e.GET("/new", NewIndex)
	e.Start(":80")

}

func NewIndex(c echo.Context) error {
	return c.String(http.StatusOK, "Hello, World! NewIndex")
}

func CustomRewriteConfig() middleware.RewriteConfig {
	return middleware.RewriteConfig{
		Rules: map[string]string{
			"/old":              "/new",
			"/api/*":            "/$1",
			"/js/*":             "/public/javascripts/$1",
			"/users/*/orders/*": "/user/$1/order/$2",
		},
	}
}
